<script setup>
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { apiCancelPosting, apiGetUserPostingList } from '@/api/apis'
import { getUserId } from '@/utils/auth'
import { goPreview, goPreviewAlbum, toast } from '@/utils/common'
import CustomNavBar from '@/components/custom-nav-bar/custom-nav-bar.vue'

const postings = ref([])
const type = ref(1) // 默认类型为壁纸

const fetchPostings = () => {
  const params = {
    userId: getUserId(),
    type: type.value,
  }
  apiGetUserPostingList(params).then((res) => {
    postings.value = res.rows
    // console.log('type',type.value)
    // console.log('postings',postings.value)
  })
}

onLoad(() => {
  fetchPostings()
})

const toggleType = () => {
  type.value = type.value === 1 ? 2 : 1
  fetchPostings()
}

const postStatus = computed(() => {
  return postings.value.map((post) => {
    switch (post.status) {
      case 0:
        return '审核中'
      case 1:
        return '已发布'
      case 2:
        return '未通过'
      default:
        return '审核中'
    }
  })
})

const cancelPublish = (id,type) => {
  // 提示用户是否确认取消发布
  uni.showModal({
    title: '提示',
    content: '确定要取消发布吗？',
    success: (res) => {
      if (res.confirm) {
        // 取消发布
        // console.log(id)
        const params = {
          itemId: id,
          userId: getUserId(),
          type: type,
        }
        apiCancelPosting(params).then((res) => {
          toast('取消发布成功')
          // 刷新列表
          fetchPostings()
        })
      }
    },
  })
}
// 返回上一页的方法
const goBack = () => {
  uni.navigateBack({
    delta: 1,
  })
}
const goPublish = (id, type) => {
  // 跳转到发布页面
  uni.reLaunch({
    url: `/pages/publish/publish?id=${id}&type=${type}`,
  })
}
</script>

<template>
  <view class="postings-container">
    <!-- //返回按钮
    <view class="back-button" @click="goBack">
      <uni-icons type="back" size="24" color="#333"></uni-icons>
      <text>返回</text>
    </view> -->
    <button @click="toggleType" class="toggle-button">
      切换到{{ type === 1 ? '专辑' : '壁纸' }}
    </button>

    <view v-if="postings.length === 0" class="empty-state">
      <text>暂无发布内容</text>
    </view>

    <view v-else class="posts-list">
      <view v-for="(post, index) in postings" :key="index" class="post-item">
        <image v-if="type === 1" :src=" post.smallPicurl" alt="图片" class="post-image" />
        <image v-else :src=" post.imgUrl" alt="图片" class="post-image" />
        <view class="post-details">
          <text class="post-title">{{ post.description }}</text>
          <text v-if="post.score" class="post-score">评分: {{ post.score }}</text>
          <text v-else class="post-score">暂无评分</text>
          <text class="post-status">{{ postStatus[index] }}</text>
        </view>
        <view class="btn-group">
          <button @click="type === 1 ? goPreview(post.id) : goPreviewAlbum(post.id)">预览</button>
          <button @click="goPublish(post.id, type)">编辑</button>
          <button @click="cancelPublish(post.id)">删除</button>
        </view>
      </view>
    </view>
  </view>
</template>
<style scoped lang="scss">
.postings-container {
  padding: 20px;
  background-color: #f9f9f9;
  min-height: 100vh;

  .back-button {
    position: absolute;
    top: 20rpx;
    left: 20rpx;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.1);
    }

    text {
      margin-left: 5rpx;
      font-size: 28rpx;
      color: #333;
    }
  }
  .header {
    font-size: 24px;
    font-weight: bold;
    color: #555;
    margin-bottom: 20px;
    text-align: center;
  }

  .toggle-button {
    margin: 10px auto;
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;

    &:hover {
      background-color: #0056b3;
    }
  }

  .empty-state {
    text-align: center;
    margin-top: 50px;
    color: #999;
    font-size: 18px;
  }

  .posts-list {
    width: 100%;
    display: block; // 确保每行只有一个卡片
  }

  .post-item {
    background-color: #fff;
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    width: 100%; // 每个卡片占满整个宽度
    margin-bottom: 20px; // 卡片之间有间距
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    border: 1px solid #e0e0e0; // 添加边框

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
      border-color: #007bff; // 悬停时改变边框颜色
    }

    .post-image {
      display: block;
      border-radius: 8px;
      height: 300px; // 图片高度固定
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      transition: opacity 0.3s ease;
      object-fit: contain; /* 确保图片完整显示 */
    }

    .post-image:hover {
      opacity: 0.8; /* 鼠标悬停时，图片变暗 */
    }

    .post-details {
      .post-title {
        font-size: 18px;
        color: #333;
        margin-bottom: 5px;
        font-weight: bold; // 加粗标题
      }

      .post-score {
        font-size: 16px;
        color: #ff9800;
        font-weight: bold;
      }

      .post-status {
        font-size: 14px;
        color: #666;
        margin-top: 5px;
        padding: 5px;
        background-color: #f0f0f0; // 添加背景色
        border-radius: 5px; // 圆角
      }
    }
  }
}
</style>
